package top.lyc829.mymusic

import android.app.Activity
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.core.view.ViewCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import com.kunminx.architecture.ui.callback.UnPeekLiveData
import com.lzx.starrysky.StarrySky
import com.lzx.starrysky.utils.showToast
import top.lyc829.mymusic.ext.destroyLoading
import top.lyc829.mymusic.ext.dismissLoading
import top.lyc829.mymusic.ext.getTopFragment
import top.lyc829.mymusic.ext.showLoading
import top.lyc829.mymusic.interceptor.PermissionInterceptor
import top.lyc829.mymusic.ui.fragment.main.MainFragment
import top.lyc829.mymusic.viewmodel.AppViewModel

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //transparentStatusBar(this)

        StarrySky.addInterceptor(PermissionInterceptor(this))

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragment_container_view) as NavHostFragment
        val navController = navHostFragment.navController
        //NavigationUI.setupActionBarWithNavController(this, navController)

        val viewModel :AppViewModel by viewModels()

        viewModel.isLoading.observe(this) {
            if (it) {
                showLoading()
            } else {
                dismissLoading()
            }
        }
    }

    private fun transparentStatusBar(activity: Activity) {
        val window = window
        window.clearFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        )
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
    }

//    override fun onSupportNavigateUp(): Boolean {
//        val navHostFragment =
//            supportFragmentManager.findFragmentById(R.id.fragment_container_view) as NavHostFragment
//        val navController = navHostFragment.navController
//        return navController.navigateUp()
//    }

//    override fun onBackPressed() {
//        moveTaskToBack(false)
//    }

    override fun onStop() {
        Log.d("myTag", "stop")
        super.onStop()
    }

    override fun onDestroy() {
        Log.d("myTag", "destroy")
        destroyLoading()
        StarrySky.with().clearPlayerEventListener()
        super.onDestroy()
    }

    override fun onBackPressed() {
        if (getTopFragment(MainFragment::class.java) != null) {
            moveTaskToBack(false)
        } else {
            super.onBackPressed()
        }
    }
}